
#include "rdesktops.h"

RDesktops::RDesktops()
{
    profileManager = new FsProfileManager( getStdString(getUserDir() + L"/.rdesktops") );
}

void RDesktops::initialize()
{
    loadProfileList();
}

void RDesktops::RDesktops::quit()
{
    if (bClosing) return;

    if (m_taskBarIcon)
        m_taskBarIcon->RemoveIcon();

    bClosing = true;
    delete m_singleInstanceChecker;

    Close();
}

void RDesktops::getProfileList(wxArrayString& asFile)
{
    Profiles profiles = profileManager->getProfiles();
    sort( profiles.begin(), profiles.end() );

    Profiles::const_iterator it;
    for(it = profiles.begin(); it != profiles.end(); it++)
    {
        string strFileName = *it;
        asFile.Add( getWxString(strFileName) );
    }
}

void RDesktops::loadProfileList()
{
    wxArrayString asFile;
    getProfileList(asFile);

    wxListBox* lstFile = getProfileListBox();

    if (lstFile)
        lstFile->Set(asFile);
}

bool RDesktops::execute(string strProfileName)
{
    Arguments arguments = profileManager->getArguments(strProfileName);
    string strCmdLine = ProfileManager::getCommandLine(arguments);

    return wxExecute( getWxString(strCmdLine) );
}

void RDesktops::onProfileSelect(wxCommandEvent& event)
{
    string strProfileName = getStdString( event.GetString() );
    Arguments arguments = profileManager->getArguments(strProfileName);

    ReplaceAll(strProfileName, ".sh$", "");
    getNameTextCtrl()->SetValue( getWxString( strProfileName ) );

    getHostTextCtrl()->SetValue( getWxString( arguments[RDT_PARAM_SERVER] ) );
    getDomainTextCtrl()->SetValue( getWxString( arguments[RDT_PARAM_DOMAIN] ) );
    getUserTextCtrl()->SetValue( getWxString( arguments[RDT_PARAM_USER_NAME] ) );
    getPasswordTextCtrl()->SetValue( getWxString( arguments[RDT_PARAM_PASSWORD] ) );
    getAttachConsoleCheckBox()->SetValue( arguments.find(RDT_PARAM_ATTACH_CONSOLE) != arguments.end() );
}

void RDesktops::onProfileConnect(wxCommandEvent& event)
{
    string strProfileName = getStdString( event.GetString() );
    execute(strProfileName);
}

void RDesktops::onProfileSave(wxCommandEvent& event)
{

}
